package com.yvdelx.cyclowomen.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.activity.listener.ProfileActivityListener;
import com.yvdelx.cyclowomen.mediation.CycleManagement;
import com.yvdelx.cyclowomen.object.Profile;
import com.yvdelx.cyclowomen.utility.ActivityUtility;
import com.yvdelx.cyclowomen.utility.LogUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;
import com.yvdelx.cyclowomen.utility.ViewUtility;

/**
 * Profile activity display profile information and permit modification
 */
public class ProfileActivity extends Activity {

    Boolean isModification = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        ActivityUtility.addActivity(this);

        ProfileActivityListener listener = new ProfileActivityListener(this);

        // Add the listener for the validate button
        ViewUtility.setViewOnClickListener(findViewById(R.id.ValidateProfileButton), listener);

        // Get bundle from the previous activity
        Bundle bundleCalendarActivity = this.getIntent()
                .getBundleExtra(StringUtility.CST_BUNDLE);

        if(bundleCalendarActivity != null) {
            isModification = bundleCalendarActivity
                    .getBoolean(StringUtility.CST_IS_MODIFICATION);
            // In the case of profile modification (profile exist), the information is displayed
            // in the view
            if(isModification){
                Profile profile;
                try {
                    profile = CycleManagement.getProfile(); // Get profile, one profile by app
                    // Put data in the view
                    ViewUtility.setTextInEditText(this.findViewById(R.id.NameEditText),
                            String.valueOf(profile.getName()));
                    ViewUtility.setTextInTextView(this.findViewById(R.id.FirstnameEditText),
                            profile.getFirstname());
                } catch (Exception e) {
                    LogUtility.error(this.getClass(),e.getMessage(),e);
                    this.finish();
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        // If it's modification user can return on the previous activity
        if(isModification)
            ActivityUtility.closeActivity(this);
        else // else it's not permit to close before the end of the process
            Toast.makeText(this,R.string.action_unautorized,Toast.LENGTH_LONG).show();
    }
}
